/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-27
*/
package com.rzico.account.controller.member;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.account.entity.CardBill;
import com.rzico.account.service.CardBillService;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Card;
import com.rzico.basics.entity.Member;
import com.rzico.basics.entity.Vip;
import com.rzico.basics.service.CardService;
import com.rzico.basics.service.MemberService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员卡账单控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-27
 */
@Api(description = "会员卡账单接口")
@RestController
@RequestMapping("/member/card_bill")
public class CardBillMemberController extends BaseController {

    @Autowired
    private CardBillService cardBillService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private CardService cardService;

    /**
     * 分页查询会员卡账单
     *
     * @return
    */
    @ApiOperation("分页查询会员卡账单")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "cardId", value = "会员卡id", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "month", value = "月份", dataType = "String", paramType = "query")
    })
    public CommResult<CardBill> list(Long cardId,String month, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        Member member = memberService.getCurrent();

        params.put("memberId", member.getId());
        params.put("cardId", cardId);
        if (month!=null) {
            String m = month.concat("-01");
            try {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date d = sdf.parse(m);
                d = DateUtils.addMonths(d, 1);
                d = DateUtils.addMilliseconds(d, -1);
                params.put("endDate", d);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<CardBill> list = cardBillService.selectList(params);
        PageResult<CardBill> pageResult = new PageResult<CardBill>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }


    /**
     * 扫码激活福利卡
     *
     * @return
     */
    @ApiOperation("扫码激活福利卡")
    @GetMapping("/scaner")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "二维码值", dataType = "String", paramType = "query")
    })
    public CommResult<Card> scaner(String q) {

        Member member = memberService.getCurrent();
        if (member==null) {
            return CommResult.error("没有登录");
        }

        //https://kdfdkfjdk/q/88123456

        Integer w = q.indexOf("/q/88");
        String code = q.substring(w+5);

        Map<String,Object> params = new HashMap<>();
        params.put("code",code);
        params.put("enterpriseId",member.getEnterpriseId());
        List<Card> cardList = cardService.selectList(params);

        Card card = null;
        if (cardList.size()==0) {
            return CommResult.error("无效二维码");
        }
        card = cardList.get(0);

        if (!card.getStatus().equals(0)) {
            return CommResult.error("已经被领取了");
        }

        if (card.getMemberId()!=null) {
            return CommResult.error("已经被领取了");
        }


        cardBillService.scaner(card,member);

        return CommResult.success("激活成功");

    }

}
